/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.UserBean;
import ejb.UserBeanLocal;
import entities.User;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.component.dialog.Dialog;
import org.primefaces.component.effect.Effect;
import utils.UtilsBean;

/**
 *
 * @author Alejandro
 */
@ManagedBean
@RequestScoped
public class SignupBean {

    private Dialog signupDialog;
    private String pass2;
    @EJB
    private UserBeanLocal userBean;
    private User user;

    public SignupBean() {
        this.userBean = new UserBean();
        this.user = new User();
        this.signupDialog = new Dialog();
     //    id="modalDialog" header="Sign up!" showEffect="explode" hideEffect="bounce" widgetVar="signup" modal="true" height="100" resizable="false" 
        this.signupDialog.setId("modalDialog");
        this.signupDialog.setHeader("Sign up!");
        this.signupDialog.setShowEffect("explode");
        this.signupDialog.setHideEffect("bounce");
        this.signupDialog.setWidgetVar("signup");
        this.signupDialog.setModal(true);
        this.signupDialog.setWidth("100");
        this.signupDialog.setResizable(false);
        
    }

    public UserBeanLocal getUserBean() {
        return userBean;
    }

    public void setUserBean(UserBeanLocal userBean) {
        this.userBean = userBean;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getPass2() {
        return pass2;
    }

    public void setPass2(String pass2) {
        this.pass2 = pass2;
    }

    public Dialog getSignupDialog() {
        return signupDialog;
    }

    public void setSignupDialog(Dialog signupDialog) {
        this.signupDialog = signupDialog;
    }
    public String goToSignup(){
        return "SIGNUP";
    }
    public String signup() {
        Boolean fatalError = false;
        if(user.getEmail()==null || user.getEmail().isEmpty()){
           fatalError = true;
        }
        if(user.getName()==null || user.getName().isEmpty()){
              fatalError = true;
        }
        if(user.getSurname()==null || user.getSurname().isEmpty()){
              fatalError = true;
        }
        if(user.getPhone()==null || user.getPhone().isEmpty()){
              fatalError = true;
        }
        if(user.getEmail()==null || user.getEmail().isEmpty()){
              fatalError = true;
        }
        Boolean error= false;
        if(fatalError){
             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Error no esperado"));
        }else{
            if(user.getPass()==null || user.getPass().isEmpty() || pass2==null || pass2.isEmpty() ||  !user.getPass().equals(pass2) || user.getPass().length()<6 || user.getPass().length()>15){
                 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Las contraseñas deben coincidir y ser superior a 6 carácteres e inferior a 15", "Escribalas de nuevo"));
            error =true;
            }            
        }
        User aux = userBean.findByEmail(user);
        if(aux!=null && aux.getStatus().equals(1)){//El usuario ya existe
           FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"El email ya está registrado", "Intentelo con otro email"));
           error=true; 
        }else if(aux!=null && aux.getStatus().equals(0)){
//            user.setStatus(1);
//            user.setName(pass2);
//            user.setSurname(pass2);
//            user.setPhone(pass2);
//            user.setAddress(pass2);
//            user.setPass(pass2);
            user.setPermission(aux.getPermission());            
        }
        
        if(!error){
            user.setPass(UtilsBean.sha512(user.getPass()));
            userBean.addUser(user);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Se ha registrado correctamente!"));
            return "LOGIN";
        }else{
            return "ERROR";
        }
    }
}
